package com.lms.web;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.lms.domain.NewProjects;
import com.lms.domain.NewTraining;
import com.lms.domain.Project;
import com.lms.domain.Role;
import com.lms.domain.Training;
import com.lms.domain.User;
import com.lms.dto.NewTrainingDto;
import com.lms.dto.NewTrainingListDto;
import com.lms.dto.NewTrainingMapper;
import com.lms.dto.ProjectDto;
import com.lms.dto.ProjectMapper;
import com.lms.dto.UserDto;
import com.lms.dto.UserListDto;
import com.lms.dto.UserMapper;
import com.lms.service.NewTrainingService;

@Controller
public class TrainingController extends MultiActionController {
	
	@Autowired
	private com.lms.service.UserService service;
	
	@Autowired
	private NewTrainingService trainingService;
	
	@RequestMapping(value="/scheduletraining",method = RequestMethod.GET)
	public String scheduletraining(ModelMap model) {
 
		//model.addAttribute("message", "Spring 3 MVC Hello World");
		return "scheduletraining";
 
	}

	@RequestMapping(value="/recordsT")
	public @ResponseBody UserListDto getUsersT() {

	UserListDto userListDto = new UserListDto();
	userListDto.setUsers(UserMapper.map(service.readAll()));
	return userListDto;
	}
	
	@RequestMapping(value="/updateT", method=RequestMethod.POST)
	public @ResponseBody UserDto updateT(
	@RequestParam String username,
	@RequestParam String firstName,
	@RequestParam String lastName,
	@RequestParam Integer role,
	@RequestParam Integer project,
	@RequestParam Integer training) {

	Role existingRole = new Role();
	existingRole.setRole(role);

	Project existingProject = new Project();
	existingProject.setProject(project);
	
	Training existingTraining = new Training();
	existingTraining.setTraining(training);

	User existingUser = new User();
	existingUser.setUsername(username);
	existingUser.setFirstName(firstName);
	existingUser.setLastName(lastName);
	existingUser.setRole(existingRole);
	existingUser.setProject(existingProject);
	existingUser.setTraining(existingTraining);

	return UserMapper.map(service.update(existingUser));
	}
	
	@RequestMapping(value="/recordsTraining")
	public @ResponseBody NewTrainingListDto getUsersP() {

		NewTrainingListDto trainingListDto = new NewTrainingListDto();
		trainingListDto.setTrainings(NewTrainingMapper.map(trainingService.readAll1()));
	return trainingListDto;
	}
	
	@RequestMapping(value = "/createTraining", method = RequestMethod.POST)
	public @ResponseBody NewTrainingDto createTraining(
			@RequestParam String trainingId,
			@RequestParam String trainingName,
			@RequestParam String description) {

		try{
			NewTraining newTraining = new NewTraining();
			newTraining.setTrainingId(trainingId);
			newTraining.setTrainingName(trainingName);
			newTraining.setDescription(description);

			System.out.println("\n\n\n\n\nhhhhhhhhhhiiiiiiii\n\n\n\n" + newTraining.getTrainingId());

			return NewTrainingMapper.map(trainingService.createTraining(newTraining));
		}
		
		catch(Exception e){
			return null;
		}
	}
	
	@RequestMapping(value = "/updateTraining", method = RequestMethod.POST)
	public @ResponseBody NewTrainingDto updateTraining(
			@RequestParam String trainingId,
			@RequestParam String trainingName,
			@RequestParam String description) {
		
		System.out.println("\n\n\n\n\n\n\n\n\n\n\n");
		System.out.println(trainingName);
		System.out.println("\n\n\n\n\n\n\n\n\n\n\n");
		
		NewTraining updateTraining = new NewTraining();
		updateTraining.setTrainingId(trainingId);
		updateTraining.setTrainingName(trainingName);
		updateTraining.setDescription(description);

		return NewTrainingMapper.map(trainingService.updateTraining(updateTraining));
	}
	
	@RequestMapping(value="/deleteTraining", method=RequestMethod.POST)
	public @ResponseBody Boolean deleteTraining(
	@RequestParam String trainingId) {

	NewTraining existingTraining = new NewTraining();
	existingTraining.setTrainingId(trainingId);

	return trainingService.delete(existingTraining);
	}

}
